package com.shy.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shy.config.common.BaseQuery;
import com.shy.config.common.ExcelDataListener;
import com.shy.config.util.AuthUtils;
import com.shy.entity.Clue;
import com.shy.entity.ClueRemark;
import com.shy.mapper.ClueMapper;
import com.shy.mapper.ClueRemarkMapper;
import com.shy.service.ClueService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {
    @Resource
    private ClueMapper clueMapper;
    @Resource
    private ClueRemarkMapper clueRemarkMapper;

    /**
     * 分页查询所有线索
     *
     * @param current
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Clue> queryClueList(Integer current, Integer pageSize) {
        //设置分页参数
        PageHelper.startPage(current, pageSize);
        // 分页查询
        List<Clue> clueList = clueMapper.queryClueListByPage(new BaseQuery());
        // 创建分页对象
        PageInfo<Clue> pageInfo = new PageInfo<>(clueList);
        return pageInfo;
    }

    /**
     * 根据id查询线索详情
     */
    @Override
    public Clue queryClueById(Integer clueId) {
        return clueMapper.selectByClueKey(clueId);
    }

    /**
     * 根据id查询线索备注
     */
    @Override
    public List<Clue> queryClueByIds(Integer clueId) {
        return clueRemarkMapper.selectByPrimaryId(clueId);
    }

    /**
     * 批量导入线索
     *
     * @param file
     */
    @Override
    public void batchImportClue(MultipartFile file) throws IOException {
//        fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(file.getInputStream(), Clue.class, new ExcelDataListener(clueMapper)).sheet().doRead();

    }

    /**
     * 添加线索
     * @param clue
     * @return
     */
    @Override
    public Boolean saveClue(Clue clue) {
        clue.setOwnerId(AuthUtils.getLoginSecurityUserId());
        clue.setCreateTime(new Date());
        clue.setCreateBy(AuthUtils.getLoginSecurityUserId());
        return clueMapper.insertSelective(clue) > 0;
    }

    /**
     * 删除
     * @param clue
     * @return
     */
    @Override
    public Boolean modifyClue(Clue clue) {
        clue.setEditTime(new Date());
        clue.setEditBy(AuthUtils.getLoginSecurityUserId());
        return clueMapper.updateByPrimaryKeySelective(clue)>0;
    }

    /**
     * 修改
     * @param ids
     * @return
     */
    @Override
    public Boolean batchRemoveCluesByIds(List<Integer> ids) {
        return clueMapper.batchDeleteCluesByIds(ids)==ids.size();
    }

    /**
     * 添加线索备注
     * @param clueRemark
     * @return
     */
    @Override
    public Boolean addActivityRemarks(ClueRemark clueRemark) {
        clueRemark.setDeleted(0);
        clueRemark.setCreateBy(AuthUtils.getLoginSecurityUserId());
        clueRemark.setCreateTime(new Date());
        return clueRemarkMapper.insertSelective(clueRemark)>0;
    }

    @Override
    public Boolean deleteRemark(Integer clueRemarkId) {
        ClueRemark clueRemark = new ClueRemark();
        clueRemark.setDeleted(1);
        clueRemark.setId(clueRemarkId);
        return clueRemarkMapper.updateByPrimaryKeySelective(clueRemark)>0;
    }
}
